import Vue from 'vue';
// HTTP工具类
export default class mpauth {
	static async getcode () {
		let [_,loginCode] = await uni.login({ provider: 'weixin'});
		return loginCode.code
	}
	// 封闭request请求
    static async gettoken(vm) {
       
		// #ifdef MP-WEIXIN
		// 如果没有全局token
        if (!vm.vuex_token) {
			let logincode = await this.getcode()
            const signpParam = {
                url: 'https://vote.readfollow.com/api/wxlogin',
                method: 'GET',
				header: {
				'content-type': 'application/json'  // 
				},
                data: {code: logincode, fromid: vm.vuex_fromid, version:vm.vuex_version, scene:vm.vuex_scene}
            };
			const [err2,signData]= await uni.request(signpParam);
			if(signData.data.code ==200){
				vm.$u.vuex('vuex_token', signData.data.data.token);
				vm.$u.vuex('vuex_uid', signData.data.data.uid);
			}
        }
		
		//#endif
    }

  /**
   * 判断请求是否成功
   */
    static isSuccess (res) {
        const wxCode = res.statusCode;
    // 微信请求错误
        if (wxCode !== 200) {
            return false;
        }
        return true;
    }

  /**
   * 异常
   */
    static requestException (res) {
        const error = {};
        error.statusCode = res.statusCode;
        const wxData = res.data;
        const serverData = wxData.data;
        if (serverData) {
            error.serverCode = wxData.code;
            error.message = serverData.message;
            error.serverData = serverData;
        }
        return error;
    }
}
